﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CanDoo;
using CanDoo.Core;
using CanDoo.Core.Data;
using CanDoo.Data;
using CanDoo.Extensions;
using CanDoo.Oil.Core.Contracts;
using CanDoo.Oil.Core.Models;
using CanDoo.Oil.Core.DTOs;

namespace CanDoo.Oil.Core.Services
{
    /// <summary>
    /// 业务实现——APP升级
    /// </summary>
    public class AppUpdateService : CanDoo.Data.Entity.Services.ServiceBase<AppUpdate>, IAppUpdateContract
    {
        public AppUpdateService()
        {
        
        }

        public VersionInfo GetPADVersion(string host)
        {
            using (DbScopeFactory.CreateReadOnly())
            {
                var entity = FirstOrDefault(p => p.AppType == "pad");
                if (entity == null)
                    return null;

                VersionInfo versionInfo = new VersionInfo();
                if (entity.ForceUpdate)
                    versionInfo.updateTag = 1;
                else
                    versionInfo.updateTag = 0;
                versionInfo.os = entity.OS;
                versionInfo.createTime = entity.CreatedTime.ToDateTime().TimeToString("yyyy-MM-dd hh:mm:ss");
                //versionInfo.updateTime = entity.LastUpdatedTime.ToDateTime().TimeToString("yyyy-MM-dd hh:mm:ss");
                versionInfo.name = entity.Name;
                versionInfo.icon = entity.LogoPath;
                versionInfo.description = entity.Remark;
                versionInfo.version = entity.Version;
                versionInfo.id = 0;// entity.Id;
                versionInfo.type = entity.AppType;
                versionInfo.url = host  + entity.FilePath.Substring(1);
                versionInfo.status = 1;
                versionInfo.md5 = entity.MD5;
                return  versionInfo;

            }
        }

        public VersionInfo GetMediaVersion(string host)
        {
            using (DbScopeFactory.CreateReadOnly())
            {
                var entity = FirstOrDefault(p => p.AppType == "ad");
                if (entity == null)
                    return null;

                VersionInfo versionInfo = new VersionInfo();
                if (entity.ForceUpdate)
                    versionInfo.updateTag = 1;
                else
                    versionInfo.updateTag = 0;
                versionInfo.os = entity.OS;
                versionInfo.createTime = entity.CreatedTime.ToDateTime().TimeToString("yyyy-MM-dd hh:mm:ss");
                //versionInfo.updateTime = entity.LastUpdatedTime.ToDateTime().TimeToString("yyyy-MM-dd hh:mm:ss");
                versionInfo.name = entity.Name;
                versionInfo.icon = entity.LogoPath;
                versionInfo.description = entity.Remark;
                versionInfo.version = entity.Version;
                versionInfo.id = 0;// entity.Id;
                versionInfo.type = entity.AppType;
                versionInfo.url = host + entity.FilePath.Substring(1);
                versionInfo.status = 1;
                versionInfo.md5 = entity.MD5;
                return versionInfo;
            }
        }
        
    }
}
